package StackAndQuene;


import java.util.Arrays;

public class PersonalQuene {//队列
    private int[] q;
    int front = 0;//队头
    int rear = 0;//队尾，说明指向队尾元素的下一个位置
    public PersonalQuene(int length){//构造方法
        this.q = new int[length];
    }
    public void enQuene(int num){//入队，后进后出
        if (this.is_full()){//表示队满了
            System.out.println("the quene is full");
            return;
        }
        this.q[this.rear] = num;
        this.rear = (this.rear+1) % this.q.length;
        System.out.println("the pushed element is:"+num);
    }

    public Integer dequene(){//出队，先进先出
        if (this.is_empty()){//如果队空
            return null;
        }
        int temp = this.q[this.front];
        this.front = (this.front+1) % this.q.length;
        return temp;
    }

    public Integer getHead(){//访问头元素
        if (this.is_empty()){//队空
            return null;
        }
        return this.q[this.front];
    }

    public Boolean is_full(){
        if((this.rear+1)%this.q.length == this.front){//如果rear下一个就是front，就是满的
            return true;
        }
        return false;
    }

    public Boolean is_empty(){
        if (this.front==this.rear){
            return true;
        }
        return false;
    }

    public void ToString(){
        System.out.println(Arrays.toString(this.q));
    }
    public Integer peek(){//获取队头元素
        return this.q[this.front];
    }
    public int size(){
        return (this.rear-this.front+this.q.length) % this.q.length;
    }
}
